package com.itlong.cloud.resource;

import com.itlong.cloud.POJO.DTO.smartcard.SmartCardSyncGetPullDataDTO;
import com.itlong.cloud.POJO.DTO.smartcard.SmartCardSyncMessageDTO;
import com.itlong.cloud.POJO.DTO.smartcard.SmartCardSyncTaskStatusDTO;
import com.itlong.cloud.POJO.PO.SmartCardPullPO;
import com.itlong.cloud.POJO.PO.SmartCardPushPO;
import com.itlong.cloud.POJO.PO.TimeTaskPullRemoveRepeatPO;
import com.itlong.cloud.POJO.PO.TimeTaskPushAnalysisPO;
import com.itlong.cloud.POJO.VO.smartcard.SmartCardPullTableInfoVO;
import com.itlong.cloud.project.smartcard.ISmartCardSyncDataServiceClient;
import com.itlong.cloud.service.ISmartCardSyncDataPullService;
import com.itlong.cloud.service.ISmartCardSyncDataPushService;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <desc>
 *      与旺龙一卡通对接服务接口实现类
 * </desc>
 *
 * @createDate 2017-11-01
 */
@RestController
public class SmartCardSyncDataServiceResource implements ISmartCardSyncDataServiceClient {
    private static final Logger LOG = LoggerFactory.getLogger(SmartCardSyncDataServiceResource.class);

    //与旺龙一卡通对接-操作一卡通push云端的数据的接口
    @Autowired
    ISmartCardSyncDataPushService iSmartCardSyncDataPushService;

    //与旺龙一卡通对接-操作一卡通pull云端的数据的接口
    @Autowired
    ISmartCardSyncDataPullService iSmartCardSyncDataPullService;


    /**
     * <desc>
     * 初步处理一卡通推送给云端的数据（放入相应的push表中并在变化标记表中登记）
     * </desc>
     *
     * @param smartCardSyncMessageDTO 一卡通推送到云端同步数据.
     * @author LewisLouis
     * @createDate 2017-12-20
     */
    @Override
    @RequestMapping(value = "smartCard/syncData/receiveData", method = RequestMethod.POST)
    public void receiveSyncDataFromSmartCard(@RequestBody SmartCardSyncMessageDTO smartCardSyncMessageDTO) throws Exception {
        iSmartCardSyncDataPushService.receiveSyncDataFromSmartCard(smartCardSyncMessageDTO);
    }

    /**
     * <desc>
     * 获取指定类型的未同步数据数据.
     * </desc>
     *
     * @param smartCardSyncGetPullDataDTO    获取推送给一卡通的数据查询条件对象
     * @return dataType
     * @author LewisLouis
     * @createDate 2017-12-25
     */
    @Override
    @RequestMapping(value = "smartCard/syncData/sendData", method = RequestMethod.POST)
    public List<SmartCardPullPO> getNotSyncData(@RequestBody SmartCardSyncGetPullDataDTO smartCardSyncGetPullDataDTO) {
        return iSmartCardSyncDataPullService.getNotSyncData(smartCardSyncGetPullDataDTO);
    }


    /**
     * <desc>
     *     一卡通请求获取云端数据之后云端确认一卡通是否同步成功
     * <desc/>
     *
     * @param projectId 项目Id
     * @param syncSerialNos 同步流水号
     * @return
     * @author zhangs
     * @createDate 2017/10/31
     */
    @RequestMapping(value = "smartCard/syncData/confirmInfo", method = RequestMethod.POST)
    @Override
    public void confirmInfo(@RequestParam("projectId") String projectId, @RequestParam("syncSerialNo") String syncSerialNos) {
        iSmartCardSyncDataPullService.confirmInfo(projectId, syncSerialNos);
    }


    /**
     * <desc>
     *     整理Pull表中数据的定时任务获取有数据变动的pull表
     * <desc/>
     *
     * @param mark 数据变动标记 0:无变动 | 1:有变动
     * @return List<TimeTaskPullRemoveRepeatPO> 定时任务临时表数据集
     * @author zhangs
     * @createDate 2017/11/3
     */
    @RequestMapping(value = "smartCard/manage/obtainPullTableName", method = RequestMethod.POST)
    @Override
    public List<TimeTaskPullRemoveRepeatPO> obtainPullTableName(@RequestParam("mark") Integer mark) {
        return iSmartCardSyncDataPullService.obtainPullTableName(mark);
    }

    /**
     * <desc>
     *     更新表time_task_pull_remove_repeat中pull表名对应的数据处理状态
     * <desc/>
     *
     * @param timeTaskPullRemoveRepeatPO 存储有数据变化的pull表信息的定时任务临时表
     * @return
     * @author zhangs
     * @createDate 2017/11/3
     */
    @RequestMapping(value = "smartCard/manage/updateTimeTaskPullStatus", method = RequestMethod.POST)
    @Override
    public void updateTimeTaskPullStatus(@RequestBody TimeTaskPullRemoveRepeatPO timeTaskPullRemoveRepeatPO) {
        iSmartCardSyncDataPullService.updateTimeTaskPullStatus(timeTaskPullRemoveRepeatPO);
    }

    /**
     * <desc>
     *     更新表time_task_push_analysis中push表名对应的数据处理状态
     * <desc/>
     *
     * @param timeTaskPushAnalysisPO 存储有数据需要解析的push表信息的定时任务临时表
     * @return
     * @author zhangs
     * @createDate 2017/11/3
     */
    @RequestMapping(value = "smartCard/syncData/updateTimeTaskPushStatus", method = RequestMethod.POST)
    @Override
    public void updateTimeTaskPushStatus(@RequestBody TimeTaskPushAnalysisPO timeTaskPushAnalysisPO) {
        iSmartCardSyncDataPushService.updateTimeTaskPushStatus(timeTaskPushAnalysisPO);
    }

    /**
     * <desc>
     *     解析Push表中数据的定时任务获取有数据变动的push表
     * <desc/>
     *
     * @param mark 数据解析标记 0:无需解析 | 1:需要解析
     * @return List<TimeTaskPushAnalysisPO> 定时任务临时表数据集
     * @author zhangs
     * @createDate 2017/11/3
     */
    @RequestMapping(value = "smartCard/syncData/getPushTableName", method = RequestMethod.POST)
    @Override
    public List<TimeTaskPushAnalysisPO> getPushTableName(@RequestParam("mark") Integer mark) {
        return iSmartCardSyncDataPushService.getPushTableName(mark);
    }

    /**
     * <desc>
     * 获取是否有任意一卡通项目推送给云端的数据需处理.
     * </desc>
     *
     * @return SmartCardSyncTaskStatusDTO 定时任务（分析一卡通推送给云端的数据）的运行状态.
     * @author LewisLouis
     * @createDate 2017-12-26
     */
    @Override
    @RequestMapping(value = "smartCard/syncData/getSmartCardSyncTaskStatus", method = RequestMethod.POST)
    public SmartCardSyncTaskStatusDTO getSmartCardSyncTaskStatus() {
        return iSmartCardSyncDataPushService.getSmartCardSyncTaskStatus();
    }

    /**
     * <desc>
     * 设置是否有任意一卡通项目推送给云端的数据需处理.
     * </desc>
     *
     * @param taskStatus 定时任务（分析一卡通推送给云端的数据）的运行状态.
     * @return
     * @author LewisLouis
     * @createDate 2017-12-26
     */
    @Override
    @RequestMapping(value = "smartCard/syncData/setSmartCardSyncTaskStatus", method = RequestMethod.POST)
    public void setSmartCardSyncTaskStatus(@RequestBody SmartCardSyncTaskStatusDTO taskStatus) {
        iSmartCardSyncDataPushService.setSmartCardSyncTaskStatus(taskStatus);
    }


    /**
     * <desc>
     *     定时任务获取pull表中未处理的数据
     * <desc/>
     *
     * @param smartCardPullPO 查询条件
     * @return List<SmartCardPullPO> 旺龙一卡通需要pull的数据集
     * @author zhangs
     * @createDate 2017/11/3
     */
//    @RequestMapping(value = "dockWith/manage/getUntreatedData", method = RequestMethod.POST)
//    @Override
//    public List<SmartCardPullPO> getUntreatedData(SmartCardPullPO smartCardPullPO) {
//        return iSmartCardSyncDataPullService.getUntreatedData(smartCardPullPO);
//    }

    /**
     * <desc>
     *     定时任务获取push表中未解析的数据
     * <desc/>
     *
     * @param tableName 查询条件
     * @return List<SmartCardPushPO> 旺龙一卡通push的数据集
     * @author zhangs
     * @createDate 2017/11/3
     */
    @RequestMapping(value = "smartCard/syncData/getUnresolvedData", method = RequestMethod.POST)
    @Override
    public List<SmartCardPushPO> getUnresolvedPushData(@RequestParam("tableName")String tableName) {
        return iSmartCardSyncDataPushService.getUnresolvedPushData(tableName);
    }

    /**
     * <desc>
     *      存储项目中各处数据更改点传输过来的数据
     * <desc/>
     *
     * @param  tableName 表名
     * @param smartCardPullPOS 与旺龙一卡通对接数据存储实体对象列表
     * @return
     * @author zhangs
     * @createDate 2017/11/3
     */
    @RequestMapping(value = "smartCard/manage/savePullData", method = RequestMethod.POST)
    public Integer batchSavePullData(@RequestParam("tableName")String tableName,
                                     @RequestBody  List<SmartCardPullPO> smartCardPullPOS) {
        return iSmartCardSyncDataPullService.batchSavePullData(tableName,smartCardPullPOS);
    }

    /**
     * <desc>
     * 条件查询推送给一卡通的数据
     * </desc>
     *
     * @param tableName       表名
     * @param smartCardPullPO 查询条件
     * @return 受影响的行数
     * @author LewisLouis
     * @createDate 2017-12-21
     */
    @Override
    @RequestMapping(value = "smartCard/manage/getPullData", method = RequestMethod.POST)
    public List<SmartCardPullPO> getPullData(@RequestParam("tableName")String tableName, @RequestBody SmartCardPullPO smartCardPullPO) {
        return iSmartCardSyncDataPullService.getPullData(tableName,smartCardPullPO);
    }


    /**
     * <desc>
     *      根据Id删除对应数据
     * <desc/>
     *
     * @param delPullIds 需要删除的pull表的Id
     * @return
     * @author zhangs
     * @createDate 2017/11/3
     */
    @RequestMapping(value = "smartCard/manage/delPullDataByIds", method = RequestMethod.POST)
    @Override
    public Integer delPullDataByIds(List<Integer> delPullIds) {
        return iSmartCardSyncDataPullService.delPullDataByIds(delPullIds);
    }

    /**
     * <desc>
     *      根据Id更新对应数据
     * <desc/>
     *
     * @param smartCardPullPO 与旺龙一卡通对接数据存储实体对象
     * @return
     * @author zhangs
     * @createDate 2017/11/3
     */
    @RequestMapping(value = "smartCard/manage/updatePullDataById", method = RequestMethod.POST)
    @Override
    public Integer updatePullDataById(@RequestParam("tableName")String tableName, @RequestBody SmartCardPullPO smartCardPullPO) {
        return iSmartCardSyncDataPullService.updatePullDataById(tableName, smartCardPullPO);
    }

    /**
     * <desc>
     *      在定时任务临时表中记录有数据变动的pull表
     * <desc/>
     *
     * @param timeTaskPullRemoveRepeatPO 定时任务临时表实体对象
     * @return
     * @author zhangs
     * @createDate 2017/11/3
     */
    @RequestMapping(value = "smartCard/manage/recordPullAtTimeTask", method = RequestMethod.POST)
    @Override
    public void recordPullAtTimeTask(TimeTaskPullRemoveRepeatPO timeTaskPullRemoveRepeatPO) {
        iSmartCardSyncDataPullService.recordPullAtTimeTask(timeTaskPullRemoveRepeatPO);
    }

    /**
     * <desc>
     *      更新push表中已完成解析的数据的状态
     * <desc/>
     *
     * @param tableName Push表名
     * @param id 数据唯一标识
     * @return
     * @author zhangs
     * @createDate 2017/11/16
     */
    @RequestMapping(value = "smartCard/syncData/setSyncPushDataResolvedById", method = RequestMethod.POST)
    @Override
    public void setSyncPushDataResolvedById(@RequestParam("tableName")String tableName, @RequestParam("id")Integer id) {
        iSmartCardSyncDataPushService.setSyncPushDataResolvedById(tableName,id);
    }

    /**
     * <desc>
     * 将已处理的一卡通同步到云端到数据进行转移.
     * </desc>
     *
     * @param tableName push表
     * @return xx
     * @author LewisLouis
     * @createDate 2018-01-09
     */
    @Override
    @RequestMapping(value = "smartCard/syncData/movePushDataToColdTable", method = RequestMethod.POST)
    public void movePushDataToColdTable(@RequestParam("tableName")String tableName) {
        iSmartCardSyncDataPushService.movePushDataToColdTable(tableName);
    }

    /**
     * <desc>
     * 将已处理的云端同步到一卡通的数据进行转移.
     * </desc>
     *
     * @param tableName push表
     * @return xx
     * @author LewisLouis
     * @createDate 2018-01-26
     */
    @Override
    @RequestMapping(value = "smartCard/syncData/movePullDataToColdTable", method = RequestMethod.POST)
    public void movePullDataToColdTable(@RequestParam("tableName")String tableName) {
        iSmartCardSyncDataPullService.movePullDataToColdTable(tableName);
    }

    /**
     * <desc>
     * 获取所有一卡通pul表的信息，用于分析是否需要清理.
     * </desc>
     *
     * @return pull表信息列表
     * @author LewisLouis
     * @createDate 2018-01-26
     */
    @Override
    @RequestMapping(value = "smartCard/syncData/getAllPullTablesInfo", method = RequestMethod.POST)
    public List<SmartCardPullTableInfoVO> getAllPullTablesInfo() {
        return iSmartCardSyncDataPullService.getAllPullTablesInfo();
    }


}
